package hotel.switchs.oversea.biz.controller;

import com.alibaba.fastjson.JSON;
import hotel.base.oversea.constant.CommonUrlConstants;
import hotel.base.oversea.constant.MeituanConstants;
import hotel.base.oversea.constant.enums.MtRequestUrlEnums;
import hotel.base.oversea.utils.Result;
import hotel.switchs.oversea.api.entity.SwitchCreateRoomEntity;
import hotel.switchs.oversea.biz.config.service.RedisService;
import hotel.switchs.oversea.biz.mapper.SwitchCreateRoomMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 执行美团涉及定时器相关数据还原或恢复
 * @author qiuyy
 * @date 2020-05-15
 */
@Slf4j
@RestController
@RequestMapping("/switchsDealDataFromJob")
@Api(value = "SwitchsDealDataFromJobController", tags = {"美团酒店订单接口"})
public class SwitchsDealDataFromJobController {
    @Autowired
    private hotel.switchs.oversea.biz.service.SwitchSubHotelInfoService SwitchSubHotelInfoService;

    @Autowired
    private SwitchCreateRoomMapper switchCreateRoomMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private RedisService redisService;

    @Autowired
    private hotel.switchs.oversea.biz.service.impl.SwitchRestTempaleUtilsService switchRestTempaleUtilsService;

    @PostMapping("/resetCreatSubJobParams")
    @ApiOperation(value = "恢复定时器美团订单详情已查询标识idsMap",notes="必须经过auth授权")
    public Result<Object> checkOrder(HttpServletRequest request){
        Map map = new HashMap<>();
        map.put("id","1");
        //Result rs  = switchRestTempaleUtilsService.sendPostRequestGetGoodsPrice( CommonUrlConstants.MEITUAN_HOST+"/" + MtRequestUrlEnums.GET_MT_HOTEL_ROOM_BY_ID.getUrl(),map);
        Map map2 = new HashMap<>();
        map2.put("id","1");
List<Integer> li = new ArrayList<>();
li.add(17552058);
        List<Integer> li2 = new ArrayList<>();
        li2.add(2147275);
        Map map3  = new HashMap();
        map3.put("goodsId",17552058);
        map3.put("startDate","2020-08-19");
        map3.put("endDate","2020-08-30");
        Result rs2  = switchRestTempaleUtilsService.sendPostRequestGetGoodsPrice( CommonUrlConstants.MEITUAN_HOST+"/"+ MtRequestUrlEnums.GET_MT_GOODS_PRICE_BY_HOTEL_ID.getUrl(),map3);

        //log.info("======rs:"+JSON.toJSONString(rs));
/*
        SendUtils.dingMsg("测试消息","测试",99,restTemplate);*/

        return new Result<>("成功",0);
    }

    @PostMapping("/resetSaleRoomAboutMtHotelIds")
    @ApiOperation(value = "售卖房型重置缓存美团酒店信息",notes="必须经过auth授权")
    public Result<Object> resetSaleRoomAboutMtHotelIds(HttpServletRequest request){
        //查询所有携程售卖房型信息
        Example example = new Example(SwitchCreateRoomEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("state", 0);
        List<SwitchCreateRoomEntity> list = switchCreateRoomMapper.selectByExample(example);
        if(null != list && list.size()>0){
            log.info("//////////  售卖房型重置缓存美团酒店信息 size:{} ",list.size(), JSON.toJSONString(list));
            Map<String,String> pushAllMtHotelIdsMap = new HashMap();
            for (SwitchCreateRoomEntity o:list){
                pushAllMtHotelIdsMap.put(String.valueOf(o.getMtHotelId()),String.valueOf(o.getMtHotelId()));
                String key_mt_hotel_id_and_goods_id=o.getMtHotelId()+"_"+o.getMtGoodId();
                String value_mt_hotel_id_and_goods_id=o.getMtRoomId()+"_"+o.getBasicRoomTypeId();//美团房型id+携程子物理房型id
                log.info("//////////  售卖房型重置缓存美团酒店信息 redisService.set :{},{}",key_mt_hotel_id_and_goods_id, value_mt_hotel_id_and_goods_id);
                //redisService.set(key_mt_hotel_id_and_goods_id,value_mt_hotel_id_and_goods_id);

                //用途：美团推送酒店异步通知上下线，判断是否属于我们上架的酒店id
                redisService.set(MeituanConstants.REIDS_MAP_ALL_SALE_HOTEL_IDS+o.getMtHotelId(),o.getMtHotelId());
                log.info("//////////  售卖房型重置缓存美团酒店id notify_ :{}",MeituanConstants.REIDS_MAP_ALL_SALE_HOTEL_IDS+o.getMtHotelId());

                //log.info("//////////  售卖房型重置缓存美团酒店信息 redisService.get :{},{}",key_mt_hotel_id_and_goods_id, redisService.get(key_mt_hotel_id_and_goods_id));
            }
            log.info("//////////  售卖房型重置缓存美团酒店信息 pushAllMtHotelIdsMap size:{} ",pushAllMtHotelIdsMap.size());
            redisService.set(MeituanConstants.REIDS_MAP_ALL_SALE_ROOM_ABOUT_MT_HOTEL_IDS,pushAllMtHotelIdsMap);
            redisTemplate.delete(MeituanConstants.REIDS_POP_ALL_SALE_ROOM_ABOUT_MT_HOTEL_IDS);
        }else{
            log.error("//////////  售卖房型重置缓存美团酒店信息 size: 0");
        }
        return new Result<>("成功",0);
    }
}
